package com.chuanye.mydrawable.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanye.mydrawable.R;
import com.chuanye.mydrawable.activitys.RemoteActivity;
import com.chuanye.mydrawable.beans.SupportUserBean;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * Created by lsef on 2019/2/14.
 */

public class DepManExpandAdapter extends BaseExpandableListAdapter{
    private Context mContext;
    private List<SupportUserBean.ObjBean> objBeanList;

    private List<SupportUserBean.ObjBean.ValueBean> objBeanList2;
    // 用来控制CheckBox的选中状况

    private List<HashMap<Integer, Boolean>> list;
    //private static HashMap<Integer, Boolean> isSelected;

    private static HashMap<String, Boolean> isSelected = new HashMap<String, Boolean>();
    private int two;

//
    public DepManExpandAdapter(Context mContext, List<SupportUserBean.ObjBean> objBeanList,HashMap<String,Boolean> isSelected,List<SupportUserBean.ObjBean.ValueBean> objBeanList2,int two) {
        this.mContext = mContext;
        this.objBeanList = objBeanList;
        this.isSelected = isSelected;
        this.objBeanList2 = objBeanList2;
        this.two = two;
        // 初始化数据
        initDate();
        Log.i("初始化：","初始化。。。");
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < objBeanList.size(); i++) {
            for (int k = 0; k < objBeanList.get(i).getValue().size(); k++){
                getIsSelected().put(objBeanList.get(i).getValue().get(k).getRealName(), false);
            }

        }
    }
    //返回一级列表的个数
    @Override
    public int getGroupCount() {
        return objBeanList.size();
    }

    //返回每个二级列表的个数
    @Override
    public int getChildrenCount(int i) {
        return objBeanList.get(i).getValue().size();
    }

    //返回一级列表的单个item（返回的是对象）
    @Override
    public Object getGroup(int i) {
        return objBeanList.get(i);
    }

    //返回二级列表中的单个item（返回的是对象）
    @Override
    public Object getChild(int i, int i1) {
        return objBeanList.get(i).getValue().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    //每个item的id是否是固定？一般为true
    @Override
    public boolean hasStableIds() {
        return true;
    }

    ////【重要】填充一级列表
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = View.inflate(mContext,R.layout.item_group, null);
        }
        TextView tv_group = (TextView) convertView.findViewById(R.id.item_group_tv);
        tv_group.setText(objBeanList.get(groupPosition).getKey());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = View.inflate(mContext,R.layout.item_child, null);
        }
        ImageView iv_child = (ImageView) convertView.findViewById(R.id.item_child_iv);
        TextView tv_child = (TextView) convertView.findViewById(R.id.item_child_tv);
        LinearLayout item_child_ll = convertView.findViewById(R.id.item_child_ll);
        tv_child.setText(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName());

        if (objBeanList2 != null && objBeanList2.size() > 0){
            for (int f = 0; f < objBeanList2.size(); f ++){
                if (objBeanList2.get(f).getRealName().equals(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName())){
                    isSelected.put(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName(), true);
                }
            }
        }

        //点击事件
        item_child_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isSelected.get(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName())) {
                    //Log.i("点击了344：",""+isSelected.get(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName()));
                    isSelected.put(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName(), false);
                    setIsSelected(isSelected);

                    if (two == 1){//远程外部 人员
                        if (RemoteActivity.valueBeanList !=null && RemoteActivity.valueBeanList.size() > 0){
                            for(int k = 0 ; k < RemoteActivity.valueBeanList.size(); k++){
                                if (RemoteActivity.valueBeanList.get(k).getRealName().equals(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName())){
                                    RemoteActivity.valueBeanList.remove(k);
                                    //部门人员 适配器刷新
                                    RemoteActivity.recySupUserAdapter.notifyDataSetChanged();
                                }
                            }
                        }
                    }else if (two == 2){//现场外部 人员
                        if (RemoteActivity.departmentmango_valueBeanList !=null && RemoteActivity.departmentmango_valueBeanList.size() > 0){
                            for(int k = 0 ; k < RemoteActivity.departmentmango_valueBeanList.size(); k++){
                                if (RemoteActivity.departmentmango_valueBeanList.get(k).getRealName().equals(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName())){
                                    RemoteActivity.departmentmango_valueBeanList.remove(k);
                                    //部门人员 适配器刷新
                                    RemoteActivity.departmentmangoAdapter.notifyDataSetChanged();
                                }
                            }
                        }
                    }

                } else {
                    //Log.i("点击了355：",""+isSelected.get(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName()));
                    isSelected.put(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName(), true);
                    setIsSelected(isSelected);
                    if (two == 1){//远程外部 人员
                       //Log.i("kkk",objBeanList.get(groupPosition).getValue().get(childPosition).getID()+"");
                        if(objBeanList.get(groupPosition).getValue().get(childPosition).getID() == 123456){//如果选择无 去掉其它
                            RemoteActivity.valueBeanList.clear();//listview
                            Set<String> set = isSelected.keySet();
                            for (String string : set){

                                if (string.equals(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName())){
                                    Log.i("kkkk",string+""+objBeanList.get(groupPosition).getValue().get(childPosition).getRealName());
                                }else {
                                    isSelected.put(string, false);
                                    setIsSelected(isSelected);
                                    Log.i("kkkk2",string+""+objBeanList.get(groupPosition).getValue().get(childPosition).getRealName());
                                }
                            }
                            notifyDataSetChanged();
                        }else {
                            for (int y =0; y < objBeanList.size(); y++){//popwindow
                                for (int k = 0; k < objBeanList.get(y).getValue().size(); k++){
                                    if (objBeanList.get(y).getValue().get(k).getID() == 123456){
                                        isSelected.put(objBeanList.get(y).getValue().get(k).getRealName(), false);
                                        setIsSelected(isSelected);
                                        notifyDataSetChanged();
                                    }
                                }
                            }
                            if (RemoteActivity.valueBeanList !=null && RemoteActivity.valueBeanList.size() > 0){//listview
                                for(int k = 0 ; k < RemoteActivity.valueBeanList.size(); k++){
                                    if (RemoteActivity.valueBeanList.get(k).getID() == 123456){
                                        RemoteActivity.valueBeanList.remove(k);
                                    }
                                }
                            }
                        }

                        RemoteActivity.valueBeanList.add(objBeanList.get(groupPosition).getValue().get(childPosition));
                        //部门人员 适配器刷新
                        RemoteActivity.recySupUserAdapter.notifyDataSetChanged();
                        RemoteActivity.crreremot_departmentman_tv.setVisibility(View.GONE);
                        RemoteActivity.crreremot_departmentman_rv.setVisibility(View.VISIBLE);
                    }else if (two == 2){//现场外部 人员

                        if(objBeanList.get(groupPosition).getValue().get(childPosition).getID() == 123456){//如果选择无 去掉其它
                            RemoteActivity.departmentmango_valueBeanList.clear();//listview
                            Set<String> set = isSelected.keySet();
                            for (String string : set){

                                if (string.equals(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName())){
                                    //Log.i("kkkk",string+""+objBeanList.get(groupPosition).getValue().get(childPosition).getRealName());
                                }else {
                                    isSelected.put(string, false);
                                    setIsSelected(isSelected);
                                    //Log.i("kkkk2",string+""+objBeanList.get(groupPosition).getValue().get(childPosition).getRealName());
                                }
                            }
                            notifyDataSetChanged();
                        }else {
                            for (int y =0; y < objBeanList.size(); y++){//popwindow
                                for (int k = 0; k < objBeanList.get(y).getValue().size(); k++){
                                    if (objBeanList.get(y).getValue().get(k).getID() == 123456){
                                        isSelected.put(objBeanList.get(y).getValue().get(k).getRealName(), false);
                                        setIsSelected(isSelected);
                                        notifyDataSetChanged();
                                    }
                                }
                            }
                            if (RemoteActivity.departmentmango_valueBeanList !=null && RemoteActivity.departmentmango_valueBeanList.size() > 0){//listview
                                for(int k = 0 ; k < RemoteActivity.departmentmango_valueBeanList.size(); k++){
                                    if (RemoteActivity.departmentmango_valueBeanList.get(k).getID() == 123456){
                                        RemoteActivity.departmentmango_valueBeanList.remove(k);
                                    }
                                }
                            }
                        }

                        RemoteActivity.departmentmango_valueBeanList.add(objBeanList.get(groupPosition).getValue().get(childPosition));
                        //部门人员 适配器刷新
                        RemoteActivity.departmentmangoAdapter.notifyDataSetChanged();
                        RemoteActivity.crreremot_departmentmango_tv.setVisibility(View.GONE);
                        RemoteActivity.crreremot_departmentmango_rv.setVisibility(View.VISIBLE);
                    }

                }
                notifyDataSetChanged();
            }
        });

        // 根据isSelected来设置checkbox的选中状况
        if (getIsSelected().get(objBeanList.get(groupPosition).getValue().get(childPosition).getRealName())){
            iv_child.setVisibility(View.VISIBLE);
        }else {
            iv_child.setVisibility(View.GONE);
        }

        return convertView;
    }

    //二级列表中的item是否能够被选中？可以改为true
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    public static HashMap<String, Boolean> getIsSelected() {
        return isSelected;

    }

    public static void setIsSelected(HashMap<String, Boolean> isSelected) {
        DepManExpandAdapter.isSelected = isSelected;
    }
}
